

#include "stdlib.h"
#include "scroll.hxx"
#include "scro.hxx"


Scroll::Scroll(int screen_width_t_, int screen_height_t_, Scroll_LevelType *level_src)
{
	level = level_src;
	tile_width  = level->tile_width;
	tile_height = level->tile_height;	
	screen_width_t = screen_width_t_;
	screen_height_t = screen_height_t_;
	screen.w = screen_width_t*tile_width;
	screen.h = screen_height_t*tile_width;
	screen.x = 0;
	screen.y = 0;
	buffer_width_t = screen_width_t+2;
	buffer_height_t = screen_height_t+2;
	if(buffer_width_t > level->level_width_t) buffer_width_t = level->level_width_t;
	if(buffer_height_t > level->level_height_t) buffer_height_t = level->level_height_t;
	if(screen_height_t > level->level_height_t) screen_height_t = level->level_height_t;
	if(screen_width_t > level->level_width_t) screen_width_t = level->level_width_t;
	buffer_x_t = level->start_x_t;
	buffer_y_t = level->start_y_t;
	buffer_surface = SDL_CreateRGBSurface(VIDEO_MODE,(buffer_width_t*tile_width), (buffer_height_t*tile_height), VIDEO_BITS, 0, 0, 0, 0);
	RefreshWholeBuffer();
}

Scroll::~Scroll()
{
	SDL_FreeSurface(buffer_surface);
}

void Scroll::RefreshWholeBuffer()
{
	SDL_Rect tile_dest;
	int x_t;
	int y_t;
	tile_dest.w=tile_width;
	tile_dest.h=tile_height;
	for(x_t = 0; x_t < buffer_width_t; x_t++)
	{
		for(y_t = 0; y_t < buffer_height_t; y_t++)
		{
			tile_dest.x=x_t*tile_width;
			tile_dest.y=y_t*tile_height;
			SDL_BlitSurface(level->tile_surface[y_t+buffer_y_t][x_t+buffer_x_t], NULL, buffer_surface, &tile_dest);
		}
	}
}

void Scroll::Blit(SDL_Surface *screen_surface)
{
	SDL_BlitSurface(buffer_surface, &screen, screen_surface, NULL);
}

void Scroll::Blit(SDL_Surface *screen_surface, SDL_Rect offset)
{
	SDL_BlitSurface(buffer_surface, &screen, screen_surface, &offset);
}

void Scroll::Move(int dx, int dy)
{
	int refresh=0;
	int new_y_pos = screen.y + dy;
	int new_x_pos = screen.x + dx;

#ifdef DEBUG
		printf("Move: dx=%d dy=%d | ",dx,dy);
    printf("new_x_pos=%d new_y_pos=%d |",new_x_pos, new_y_pos);
    printf("screen_x=%d screen_y=%d\n",screen.x, screen.y);
    printf("buffer_x_t=%d buffer_y_t=%d\n",buffer_x_t, buffer_y_t);
#endif

	if(new_x_pos < 0)
	{			
		if(buffer_x_t > 0) 
		{
			buffer_x_t-= (1 - new_x_pos / tile_width) ;
			if(buffer_x_t < 0)
			{
			  buffer_x_t = 0;
			  screen.x = 0;
			}
			else
			{
			  screen.x = (new_x_pos % tile_width) + tile_width;
			}
			refresh = 1;
		}
		else
		{
			screen.x = 0;
		}
	} 
	else if (new_x_pos > buffer_width_t*tile_width-screen.w)
	{
					
		if(buffer_x_t < level->level_width_t-buffer_width_t) 
		{
			buffer_x_t+=1 + new_x_pos / tile_width;
			if(buffer_x_t > level->level_width_t-buffer_width_t)
			{
			    buffer_x_t = level->level_width_t-buffer_width_t;
			    screen.x = tile_width;
			}
			else
			{
			    screen.x = new_x_pos % tile_width - tile_width;
			}
			refresh = 1;
		}
		else
		{
			screen.x = buffer_width_t*tile_width-screen.w;
		}			
	}
	else
	{
		screen.x = new_x_pos;
	}
	
	if(new_y_pos < 0) 
	{
		if(buffer_y_t > 0) 
		{
			buffer_y_t-=(1 - new_y_pos / tile_height);
			if(buffer_y_t < 0)
			{
			    buffer_y_t = 0;
			    screen.y = 0;
			}
			else
			{
			    screen.y = (new_y_pos % tile_height) + tile_height;
			}
			refresh = 1;
		}
		else
		{
			screen.y = 0;
		}			
	}
	else if (new_y_pos > buffer_height_t*tile_height-screen.h)
	{
		if(buffer_y_t < level->level_height_t-buffer_height_t) 
		{
			buffer_y_t+= 1 + new_y_pos / tile_height;
			if(buffer_y_t > level->level_height_t-buffer_height_t)
			{
			  buffer_y_t = level->level_height_t-buffer_height_t;
			  screen.y = tile_height;
			}
			else
			{
			  screen.y = new_y_pos % tile_height - tile_height;
			}
			refresh = 1;
		}
		else
		{
			screen.y = buffer_height_t*tile_height-screen.h;
		}				
	}
    else
	{
		screen.y = new_y_pos;
	}		
	if(refresh) RefreshWholeBuffer();			
}

